package org.blame.lotterysystem.common.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Configuration
public class RedisUtil {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final Logger logger= LoggerFactory.getLogger(RedisUtil.class);

    public boolean set(String key,String value){
        try {
            stringRedisTemplate.opsForValue().set(key,value);
            return true;
        }catch (Exception e){
            logger.error("setError key,value:{},{}",key,value,e);
            return false;
        }

    }

    public boolean set(String key,String value,Long time){
        try {
            stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            logger.error("setError key,value,time:{},{},{}",key,value,time,e);
            return false;
        }
    }

    public String get(String key){
        try {
            return StringUtils.hasText(key)?stringRedisTemplate.opsForValue().get(key):null;
        }catch (Exception e){
            logger.error("getError:{}",key,e);
            return null;
        }
    }

    public boolean deleteKey(String ... key){
        try {
            if(key!=null && key.length>0){
                if(key.length==1){
                    stringRedisTemplate.delete(key[0]);
                }else {
                    stringRedisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
                }
            }

            return true;
        }catch (Exception e){
            logger.error("deleteError:{}",key,e);
            return false;
        }
    }


    public Boolean hasKey(String key){
        try {
            if(key!=null){
                return stringRedisTemplate.hasKey(key);
            }
            return false;
        }catch (Exception e){
            logger.error("hasKeyError:{}",key,e);
            return false;
        }
    }

}
